package xy;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;

import org.dom4j.DocumentException;
import org.xml.sax.SAXException;


public class Myser extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String path = this.getServletContext().getRealPath("book.xml");
		String msg = "";
		String source = "";//判断来源，考虑是否有提示框弹出
		response.setContentType("text/html;charset=gb2312");
		String status = request.getParameter("status");
		File file = new File(path);
		
		if(status.equals("todel")){
			request.getRequestDispatcher("del.jsp").forward(request, response);
					
		}else if(status.equals("toinsert")){
			request.getRequestDispatcher("insert.jsp").forward(request, response);
					
		}else if(status.equals("tosearch")){
			request.getRequestDispatcher("search.jsp").forward(request, response);
					
		}else if(status.equals("toupdate")){
//			System.out.println(request.getParameter("id"));
			ArrayList<Student> stu = null;
			try {
				stu = new readXML().search("学号", request.getParameter("id"),1);
			} catch (XPathExpressionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ParserConfigurationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SAXException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
//			for(int i=0;i<stu.size();i++){
//			System.out.println(i+"=:"+stu.get(i).id);}
			request.setAttribute("infos", stu.get(0));
			request.getRequestDispatcher("update.jsp").forward(request, response);
					
		}else if(status.equals("dis")){
			
			if(file.exists()){
//				System.out.println(file.getAbsolutePath());
				int currentPage=Integer.parseInt(request.getParameter("currentPage"));
				request.getSession().setAttribute("currentPage", currentPage);
				int countPage=new readXML().countElement();
				request.getSession().setAttribute("countPage", countPage);
				ArrayList<Student> stus = new readXML().dis(currentPage);
				request.setAttribute("stus", stus);
				request.getRequestDispatcher("dis.jsp").forward(request, response);
			}else{
				file.createNewFile();
//				System.out.println(file.getAbsolutePath());
				msg = "文件不存在,已自动生成空文件";
				source = "信息";
				request.setAttribute("source", source);
				request.setAttribute("msg", msg);
				request.getRequestDispatcher("dis.jsp").forward(request, response);
			}
			
		}else if(status.equals("del")){
			int currentPage = 1;
			if(request.getParameter("currentPage").isEmpty()==false){
//				System.out.println(new String(request.getParameter("DelRow").getBytes("iso-8859-1"),"utf-8"));
				currentPage = Integer.parseInt(request.getParameter("currentPage"));
			}
			request.getSession().setAttribute("currentPage", currentPage);
			String tag = new String(request.getParameter("DelRow").getBytes("iso-8859-1"),"utf-8");
			String text = new String(request.getParameter("Del").getBytes("iso-8859-1"),"utf-8");
			
			new readXML().del(tag,text);
			
			int countPage=new readXML().countElement();
			request.getSession().setAttribute("countPage", countPage);
			ArrayList<Student> stus = new readXML().dis(currentPage);
			
			msg = "删除成功";
			source = "信息";
			request.setAttribute("source", source);
			request.setAttribute("Msg", msg);
			
			request.setAttribute("stus", stus);
			request.getRequestDispatcher("dis.jsp").forward(request, response);
			
		}else if(status.equals("insert")){
	
			Student stu = new Student();
			stu.setId(request.getParameter("id"));
			stu.setName(new String(request.getParameter("name").getBytes("iso-8859-1"),"utf-8"));
			stu.setJSP(request.getParameter("JSP"));
			stu.setXML(request.getParameter("XML"));
			try {
				msg = new readXML().insert(stu);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				System.out.println("新增时："+e);
			}
			
			int countPage = new readXML().countElement();
			int currentPage = countPage;
			request.getSession().setAttribute("currentPage", currentPage);
			request.getSession().setAttribute("countPage", countPage);
			ArrayList<Student> stus = new readXML().dis(currentPage);
			
			source = "信息";
			request.setAttribute("source", source);
			request.setAttribute("Msg", msg);
			
			request.setAttribute("stus", stus);
			request.getRequestDispatcher("dis.jsp").forward(request, response);
			
		}else if(status.equals("update")){
	
			Student stu = new Student();
			stu.setId(request.getParameter("id"));
			stu.setName(new String(request.getParameter("name").getBytes("iso-8859-1"),"utf-8"));
			stu.setJSP(request.getParameter("JSP"));
			stu.setXML(request.getParameter("XML"));
			try {
				msg = new readXML().update(stu);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				System.out.println(e);
			}
			
			int countPage = new readXML().countElement();
			int currentPage = 1;
			request.getSession().setAttribute("currentPage", currentPage);
			request.getSession().setAttribute("countPage", countPage);
			ArrayList<Student> stus = new readXML().dis(currentPage);
			
			source = "信息";
			request.setAttribute("source", source);
			request.setAttribute("Msg", msg);
			
			request.setAttribute("stus", stus);
			request.getRequestDispatcher("dis.jsp").forward(request, response);
			
		}else if(status.equals("search")){
			
			if(file.exists()){
				String tag = "学号" ;
				String text = "0";
				try {
					
				tag = new String(request.getParameter("SearchRow").getBytes("iso-8859-1"),"utf-8");
				text = new String(request.getParameter("Search").getBytes("iso-8859-1"),"utf-8");
				
				} catch (Exception e) {
					// TODO: handle exception
				}
				int currentPage=Integer.parseInt(request.getParameter("currentPage"));
				request.getSession().setAttribute("currentPage", currentPage);
				int countPage=new readXML().countElement();
				request.getSession().setAttribute("countPage", countPage);
				ArrayList<Student> stus = null;
				try {
					stus = new readXML().search(tag,text,1);
				} catch (XPathExpressionException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ParserConfigurationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SAXException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				if(stus.size()==0){
					msg = "未查询到该信息";
				}else
					msg = "查找成功";
				source = "信息";
				request.setAttribute("source", source);
				request.setAttribute("Msg", msg);
				
				request.setAttribute("stus", stus);
				request.getRequestDispatcher("dis.jsp").forward(request, response);
			}else{
				file.createNewFile();
				msg = "文件不存在，系统已自动生成空文件，请添加学生成绩信息。";
				source = "信息";
				request.setAttribute("source", source);
				request.setAttribute("msg", msg);
				request.getRequestDispatcher("dis.jsp").forward(request, response);
			}
			
		}
		
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}
